package com.cheejee.goodscenter.service.impl;

import static com.cheejee.goodscenter.util.StaticField.SPRCIFIC_VALUE_SPLIT;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.carry.common.tool.ListTool;
import com.cheejee.goodscenter.dao.SpecificMapper;
import com.cheejee.goodscenter.module.dto.SpecificDto;
import com.cheejee.goodscenter.module.entity.Specific;
import com.cheejee.goodscenter.service.SpecificService;
import com.cheejee.goodscenter.service.exception.DataNotExistException;
import com.cheejee.goodscenter.service.exception.DuplicateException;
import com.cheejee.goodscenter.validated.Check;
import com.cheejee.goodscenter.validated.InsertGroup;
import com.cheejee.goodscenter.validated.UpdateGroup;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.hutool.core.collection.CollectionUtil;

/**
 * @author CARRY
 * @Date 2020年10月29日下午2:34:25
 */
@Service
public class SpecificServiceImpl implements SpecificService {

	@Resource
	private SpecificMapper mapper;

	@Override
	public PageInfo<SpecificDto> querySpecific(Integer categoryId, String specificName, int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		
		return new PageInfo<SpecificDto>(convert(mapper.selectBySelective(categoryId, specificName)));
	}
	
	@Override
	public SpecificDto querySpecificById(Long id) {

		return convert(mapper.selectByPrimaryKey(id));
	}

	@Override
	@Check(message ="添加的规格不能为空", group = InsertGroup.class)
	public int addSpecific(SpecificDto specific) {
		if(!CollectionUtil.isEmpty(mapper.selectBySelective(specific.getCategoryId(), specific.getSpecificName()))) {
			throw new DuplicateException("规格名称已存在");
		}
		ListTool.checkDuplicateList(specific.getSpecificValue(), true);

		Specific c = convert(specific);
		int i = mapper.insertSelective(c);
		specific.setId(c.getId());
		
		return i;
	}

	@Override
	@Check(message ="修改的规格不能为空", group = UpdateGroup.class)
	public int changeSpecific(SpecificDto specific) {
		
		checkExist(specific.getId());
		List<Specific> list = mapper.selectBySelective(specific.getCategoryId(), specific.getSpecificName());
		if(!CollectionUtil.isEmpty(list) && list.get(0).getId() != specific.getId()) {
			throw new DuplicateException("规格名称重复");
		}
		ListTool.checkDuplicateList(specific.getSpecificValue(), true);

		return mapper.updateByPrimaryKeySelective(convert(specific));
	}

	@Override
	public int deleteSpecific(Long id) {

		return mapper.deleteByPrimaryKey(id);
	}

	private SpecificDto convert(Specific s) {
		if(s == null) {
			return null;
		}
		
		return SpecificDto.builder()
				.id(s.getId())
				.categoryId(s.getCategoryId())
				.specificName(s.getSpecificName())
				.specificValue(Arrays.asList(s.getSpecificValue().split(SPRCIFIC_VALUE_SPLIT)))
				.build();
	}
	
	private Specific convert(SpecificDto s) {
		if(s == null) {
			return null;
		}
		
		StringBuilder sb = new StringBuilder();
		s.getSpecificValue().forEach(i -> {
			sb.append(i);
			sb.append(SPRCIFIC_VALUE_SPLIT);
		});
		
		return new Specific(s.getId(), s.getCategoryId(), s.getSpecificName(), sb.substring(0, sb.length() - 1));
	}
	
	private List<SpecificDto> convert(List<Specific> s){
		List<SpecificDto> list = new ArrayList<>();
		
		if(CollectionUtil.isEmpty(s)) {
			return list; 
		}
		
		s.forEach(i -> {
			list.add(SpecificDto
					.builder()
					.id(i.getId())
					.categoryId(i.getCategoryId())
					.specificName(i.getSpecificName())
					.specificValue(Arrays.asList(i.getSpecificValue().split(SPRCIFIC_VALUE_SPLIT)))
					.build());
		});
		return list;
	}
	
	private void checkExist(Long id) {
		if(mapper.selectByPrimaryKey(id) == null) {
			throw new DataNotExistException("规格不存在");
		}
	}
}
